package com.six666.medicaldevicetestingsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.six666.medicaldevicetestingsystem.entity.ProjectFile;
import com.six666.medicaldevicetestingsystem.mapper.ProjectFile_Mapper;
import com.six666.medicaldevicetestingsystem.service.File_Service;
import com.six666.medicaldevicetestingsystem.service.ProjectFile_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectFile_Service_Impl extends ServiceImpl<ProjectFile_Mapper, ProjectFile> implements ProjectFile_Service {
    @Autowired
    File_Service file_service;
    @Override
    public String get_url_by_id(int id) {
        if (id==0){
            return "";
        }
        QueryWrapper<ProjectFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("FileID",id);
        return getOne(queryWrapper).getFileaddress();
    }
    public List<String> get_op_url_by_project_id__filetype(int project_id, String filetype){
        List<String> res = new ArrayList<>();
        QueryWrapper<ProjectFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("Filetype",filetype);
        List<ProjectFile> list =list(queryWrapper);
        for (ProjectFile projectFile : list) {
            res.add(projectFile.getFileaddress());
        }
        return res;
    }
    @Override
    public String get_cp_url_by_project_id__filetype(int project_id, String filetype){
        QueryWrapper<ProjectFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("Filetype",filetype);
        return getOne(queryWrapper).getFileaddress();
    }


    @Override
    public int isempty(int project_id) {
        QueryWrapper<ProjectFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("FileType","TrainVideo");
        queryWrapper.eq("ProjectID",project_id);
        ProjectFile res = getOne(queryWrapper);
        String Fileaddress ;
        if (res == null){
            Fileaddress = null;
        }
        else{
            Fileaddress = res.getFileaddress();}
        if(Fileaddress==null){
            return 2;
        }
        else {
            return 1;
        }
    }

    @Override
    public String gettestfile(int project_id, String filetype) {
        QueryWrapper<ProjectFile> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("FileType",filetype);
        ProjectFile res = getOne(queryWrapper);
        return res.getFileaddress();
    }

    @Override
    public boolean add_project_file(int project_id, String filetype, String file_address, String file_name) {
        ProjectFile file = new ProjectFile();
        file.setProjectid(project_id);
        file.setFileaddress(file_address);
        file.setFilename(file_name);
        file.setFiletype(filetype);
        boolean res=save(file);
        System.out.println(file.getFileid());
        return res;
    }
    @Override
    public int add_project_file_get_id(int project_id, String filetype, String file_address, String file_name) {
        ProjectFile file = new ProjectFile();
        file.setProjectid(project_id);
        file.setFileaddress(file_address);
        file.setFilename(file_name);
        file.setFiletype(filetype);
        save(file);
        return file.getFileid();
    }

    @Override
    public ProjectFile get_file_by_project_id_and_type(int project_id, String filetype) {
        QueryWrapper<ProjectFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("Filetype",filetype);
        try {
            return getOne(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    @Override
    public List<ProjectFile> get_file_by_project_id_and_type_plus(int project_id, String filetype) {
        QueryWrapper<ProjectFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("Filetype",filetype);
        try {
            return list(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String get_write(int project_id, int file_id) {
        QueryWrapper<ProjectFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("FileID",file_id);
        return getOne(queryWrapper).getFileaddress();
    }

    @Override
    public boolean delete_file_by_project_id_and_type(int project_id, String filetype) {
        QueryWrapper<ProjectFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("Filetype",filetype);
        List<ProjectFile> list =list(queryWrapper);
        for (ProjectFile projectFile : list) {
            String Fileaddress = projectFile.getFileaddress();
            file_service.delete_file(Fileaddress);
        }
        return remove(queryWrapper);
    }
    @Override
    public boolean delete_file_by_project_id_and_type_plus(int project_id,String filetype,String file_address,String file_name) {
        QueryWrapper<ProjectFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("Filetype",filetype);
        queryWrapper.eq("Fileaddress",file_address);
        queryWrapper.eq("Filename",file_name);
        return remove(queryWrapper);
    }

    @Override
    public int get_file_id_by_project_id_and_type_and_filename(int project_id, String filetype, String file_name) {
        QueryWrapper<ProjectFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("Filetype",filetype);
        queryWrapper.eq("Filename",file_name);
        ProjectFile res = getOne(queryWrapper);
        return res.getFileid();
    }

    @Override
    public String get_file_address_by_project_id_and_type(int project_id, String filetype) {
        QueryWrapper<ProjectFile> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("Filetype",filetype);
        ProjectFile res = getOne(queryWrapper);
        return res.getFileaddress();
    }

    @Override
    public boolean delete_file_by_address(String file_address) {
        QueryWrapper<ProjectFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("FileAddress",file_address);
        return remove(queryWrapper);
    }
}
